/**
 *====================================================
 * 文件名称: BulkstockServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年10月22日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockInfoEntity;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;

/**
 * @ClassName: BulkstockServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年10月22日 21:55:36
 */
@Implements(BulkstockInfoService.class)
public class BulkstockInfoServiceImpl implements BulkstockInfoService {

	@Override
	public Long createBulkstockInfo(BulkstockInfoEntity entity) {
		entity.setEnabled(Boolean.TRUE);
		entity.setUpdateDatetime(DateUtils.now());
		entity.setCreateDatetime(DateUtils.now());
		entity.setActiveState(Boolean.FALSE);
		PersistUtil.insert(entity);
		return entity.getBulkstockInfoId();
	}

	@Override
	public void updateBulkstockInfo(BulkstockInfoEntity entity) {
		BulkstockInfoEntity persist = inquireBulkstockInfo(entity.getBulkstockInfoId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateBulkstockInfo(BulkstockInfoEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public PageList<BulkstockInfoEntity> inquireBulkstockInfoPageList(Long bulkstockCategoryId, String categoryName, String bulkstockName, String specificationName, Boolean activeState, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		if (bulkstockCategoryId != null && bulkstockCategoryId != 0) {
			criteria.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		}
		if (activeState != null) {
			criteria.add(Restrictions.eq("activeState", activeState));
		}
		if (StringUtils.isNotBlank(specificationName)) {
			criteria.add(Restrictions.like("specificationName", "%" + specificationName + "%"));
		}
		if (StringUtils.isNotBlank(categoryName)) {
			criteria.add(Restrictions.like("categoryName", "%" + categoryName.trim() + "%"));
		}
		if (StringUtils.isNotBlank(bulkstockName)) {
			criteria.add(Restrictions.like("bulkstockName", "%" + bulkstockName.trim() + "%"));
		}
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		return PersistUtil.get(criteria, page);
	}

	@Override
	public BulkstockInfoEntity inquireBulkstockInfo(Long bulkstockCategoryId, String bulkstockName, String specificationName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		if (bulkstockCategoryId != null) {
			criteria.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		}
		criteria.add(Restrictions.eq("bulkstockName", bulkstockName.trim()));
		criteria.add(Restrictions.eq("specificationName", specificationName.trim()));
		List<BulkstockInfoEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
	
	@Override
	public BulkstockInfoEntity inquireBulkstockInfo(String bulkstockName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		criteria.add(Restrictions.eq("bulkstockName", bulkstockName.trim()));
		List<BulkstockInfoEntity> list = PersistUtil.get(criteria);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public BulkstockInfoEntity inquireBulkstockInfo(Long bulkstockInfoId) {
		return PersistUtil.get(BulkstockInfoEntity.class, bulkstockInfoId);
	}

	@Override
	public List<BulkstockInfoEntity> inquireBulkstockInfo(Long bulkstockCategoryId, String bulkstockName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		criteria.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		criteria.add(Restrictions.eq("bulkstockName", bulkstockName.trim()));
		return PersistUtil.get(criteria);
	}
	
	@Override
	public List<BulkstockInfoEntity> inquireBulkstockInfo() {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		return PersistUtil.get(criteria);
	}

	@Override
	@Transactional
	public void deprecatedBulkstockInfo(Long bulkstockInfoId) {
		BulkstockInfoEntity entity = inquireBulkstockInfo(bulkstockInfoId);
		entity.setUpdateDatetime(DateUtils.now());
		entity.setActiveState(Boolean.FALSE);
		renovateBulkstockInfo(entity);
	}

	@Override
	@Transactional
	public void activeBulkstockInfo(Long bulkstockInfoId) {
		BulkstockInfoEntity entity = inquireBulkstockInfo(bulkstockInfoId);
		entity.setUpdateDatetime(DateUtils.now());
		entity.setActiveState(Boolean.TRUE);
		renovateBulkstockInfo(entity);
	}

	@Override
	public void uniqueness(String bulkstockName, String specificationName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockInfoEntity.class);
		criteria.add(Restrictions.eq("specificationName", specificationName));
		criteria.add(Restrictions.eq("bulkstockName", bulkstockName.trim()));
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		List<BulkstockInfoEntity> list = PersistUtil.get(criteria);
		if (list.size()>1) {
			throw new BusinessException("大宗商品不能重复");
		}
		
	}
}
